import axios from "axios";

export const baseUrl = '/api'


export const request = (api, method, params = {}, config, responseType = {}) => {
    // apiToken = getCookie()?getCookie():localStorage.getItem('token')
    const data = method === "GET" ? "params" : "data";
    let headers = {};
    if (config?.headers) {
        headers = {
            ...headers,
            ...config.headers,
        };
    }

    return new Promise((resolve, reject) => {
        axios({
            ...config,
            url: baseUrl + api,
            method,
            [data]: params,
            headers,
            ...responseType,
        })
            .then((res) => {
                if (res.data.code !== 200) {
                    alert(res.data.message)
                    return
                }
                resolve(res.data);
            })
            .catch((error) => {
                reject(error)
            });
    });
};

